<!--
  speedy-vision.js
  GPU-accelerated Computer Vision for JavaScript
  Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  webcam-demo.html
  Feature detection using a webcam
-->
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
        <meta name="author" content="Alexandre Martins">
        <title>Webcam feature detection</title>
        <link href="demo-base.css" rel="stylesheet">
        <script src="demo-base.js"></script>
        <script src="../dist/speedy-vision.js"></script>
    </head>
    <body>
        <h1>Webcam demo</h1>
        <form autocomplete="off">
            <div>
                Sensitivity
                <input type="range" min="0.0" max="0.99" value="0.80" step="0.01" id="sensitivity">
            </div>
            <div>
                <label for="enhance-illumination">Fix uneven illumination</label>
                <input type="checkbox" id="enhance-illumination">
            </div>
            <div>
                Method:
                <select id="method">
                    <option value="orb" selected>ORB</option>
                    <option value="orbharris">ORB-Harris</option>
                    <option value="brisk" disabled>BRISK (soon)</option>
                </select>
            </div>
        </form>
        <div>
            <span id="status"></span>
            <canvas id="canvas-demo"></canvas>
        </div>
        <script>
window.onload = async function()
{
    try {
        // webcam access
        const media = await Speedy.camera();

        // form controls
        const method = document.getElementById('method');
        const sensitivity = document.getElementById('sensitivity');
        const enhanceIllumination = document.getElementById('enhance-illumination');

        // create the pipelines
        function orbWith(detector)
        {
            const pipeline = Speedy.Pipeline();
            const source = Speedy.Image.Source();
            const greyscale = Speedy.Filter.Greyscale();
            const nightvision = Speedy.Filter.Nightvision();
            const mux = Speedy.Image.Multiplexer('mux');
            const pyramid = Speedy.Image.Pyramid();
            const blur = Speedy.Filter.GaussianBlur(); // reduce noise before computing the descriptors
            const blurredPyramid = Speedy.Image.Pyramid();
            const clipper = Speedy.Keypoint.Clipper();
            const descriptor = Speedy.Keypoint.Descriptor.ORB();
            const sink = Speedy.Keypoint.Sink();

            source.media = media;
            nightvision.gain = 0.9; // increase contrast
            nightvision.offset = 0.5;
            nightvision.decay = 0.0;
            nightvision.quality = 'low';
            blur.kernelSize = Speedy.Size(9, 9);
            blur.sigma = Speedy.Vector2(2, 2);
            detector.levels = 12; // pyramid levels
            detector.scaleFactor = 1.19; // approx. 2^0.25
            detector.capacity = 8192;
            clipper.size = 800; // up to how many features?

            source.output().connectTo(greyscale.input());
            greyscale.output().connectTo(mux.input('in0'));
            greyscale.output().connectTo(nightvision.input());
            nightvision.output().connectTo(mux.input('in1'));

            mux.output().connectTo(pyramid.input());
            pyramid.output().connectTo(detector.input());
            detector.output().connectTo(clipper.input());
            clipper.output().connectTo(descriptor.input('keypoints'));

            greyscale.output().connectTo(blur.input());
            blur.output().connectTo(blurredPyramid.input());
            blurredPyramid.output().connectTo(descriptor.input('image'));

            descriptor.output().connectTo(sink.input());

            pipeline.init(source, greyscale, nightvision, mux, pyramid, blur, blurredPyramid, detector, clipper, descriptor, sink);

            return pipeline;
        }

        const fast = Speedy.Keypoint.Detector.FAST();
        const harris = Speedy.Keypoint.Detector.Harris();
        const pipelines = {
            orb: orbWith(fast),
            orbharris: orbWith(harris),
            brisk: null,
        };

        // Main loop
        (function() {
            let keypoints = [], frameReady = false;
            const canvas = setupCanvas('canvas-demo', media.width, media.height);
            canvas.style.width = (2 * media.width) + 'px';

            async function update()
            {
                // pick a pipeline
                const pipeline = pipelines[method.value];

                // enhance illumination?
                const mux = pipeline.node('mux');
                mux.port = enhanceIllumination.checked ? 1 : 0;

                // adjust the sensitivity
                fast.threshold = 255 * (1.0 - Number(sensitivity.value));
                harris.quality = 1.0 - Number(sensitivity.value);

                // find the features
                const result = await pipeline.run();
                keypoints = result.keypoints;

                // repeat
                frameReady = true;
                setTimeout(update, 1000 / 60);
            }
            update();

            function render()
            {
                if(frameReady) {
                    renderMedia(media, canvas);
                    renderKeypoints(canvas, keypoints, 'yellow', 2, 2);
                }

                frameReady = false;
                requestAnimationFrame(render);
            }
            render();

            setInterval(() => renderStatus(keypoints), 200);
        })();
    }
    catch(err) {
        console.error(err);
        alert(err.message);
    }
}
        </script>
        <section id="speedy-vision">
            <span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
            <a href="https://github.com/sponsors/alemart" target="_blank" class="button">
                <img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
            </a>
        </section>
    </body>
</html>